package com.weilive.push.service;

import java.util.Set;

import javax.annotation.PostConstruct;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.weilive.core.cache.redis.RedisConfig;
import com.weilive.core.cache.redis.RedisService;
import com.weilive.core.executor.ThreadPoolExecutor;
import com.weilive.push.gate.PusherServlet;

/**
 * 推送api
 * 
 * @author houzhiwei
 *
 */
@Service
public class PusherService {
	private static final Logger logger = LoggerFactory.getLogger(PusherServlet.class);

	private static final int PUSH_FANS = 1;
	private static final int PUSH_ALL = 2;

	@Autowired
	private RedisService redis;

	@PostConstruct
	public void init() {
		redis.exists("weilive.key.push");
		logger.info("redis初始化成功!");
	}

	/**
	 * 
	 * @param userId
	 *            开播主播id
	 * @param typeId
	 *            类型 1:关注的,2:全部
	 */
	public void push(long userId, int typeId) {
		logger.info("推送服务开始--!");

		if (!redis.exists(RedisConfig.USER_ROOM_PREF + userId)) {
			logger.warn("用户房间在redis中不存在:key=" + RedisConfig.USER_ROOM_PREF + userId);
			return;
		}
		String userAlias = redis.hget(RedisConfig.USER_ROOM_PREF + userId, RedisConfig.USER_ROOME_USER_ALIAS);

		if (userAlias.equals("")) {
			userAlias = "有";
		}

		String msg = userAlias + "主播节目开播啦，听说长得好看的都在看了，就等你哦亲~ I want u now！";

		// 自己不推送
		String userToken = redis.get(RedisConfig.USER_DEV_TOKEN_PREF + userId);

		logger.info("推送消息=" + msg + ",userToken=" + userToken);

		if (typeId == PUSH_FANS) {

			Set<String> tokens = redis.getUserTokens(RedisConfig.USER_FANS_ME_PREF + userId);

			tokens.remove(userToken);

			PushTask task = new PushTask(msg, userId, tokens);

			ThreadPoolExecutor.execute(task);

			logger.info("推送token=" + tokens);
		} else if (typeId == PUSH_ALL) {

			Set<String> allTokens = redis.getUserTokens(RedisConfig.USER_DEV_TOKEN_ANDROID);
			Set<String> tokens = redis.getUserTokens(RedisConfig.USER_DEV_TOKEN_IOS);

			allTokens.addAll(tokens);
			allTokens.remove(userToken);

			PushTask task = new PushTask(msg, userId, allTokens);

			ThreadPoolExecutor.execute(task);

			logger.info("推送token=" + allTokens);
		}
		// else if (typeId == 3) {
		// Set<String> tokens = new HashSet<String>();
		//
		// String s = "1:Aho6iRAmJab7m5W3-iQJYSPPuGXVXCQ8EtfGWEoaIkyr,
		// 0:c2e73787dbee742d2571672c6847db7c6e7273eeb4a3fb9f2f52cfe6e807dcbc,
		// 0:0f02bfa9cf2c47f378c86ab32ba8b49c2a92568a934afedd083528c79fa42bad,
		// 1:Atq2093rEqk-IONuVxxIUUG1QSRdjjinjRj0EDNc6v9b,
		// 1:AkmLIU4u11Pr0EJ831Vf_rqKPGl12CN-vwxxSmeyYWjv,
		// 1:AoDuj4EhGtdJdCq2Dcpwizg03NC-iJKuj45bm5JffKuF,
		// 0:f6329e05a3c5ff4209782f176e7d14147d84d6c4bd5476fb3aaf0dbf5e7f87f8,
		// 1:AspmX7CCO_rlm0zi79zUUHxc-MrtdL45txpXJ0yjH9wK,
		// 1:AkwYY70MjVeSE4mbIK-alOfLKZpSgnBhmlvaUPbLaHXY,
		// 0:daa0d7aa49435969a80687c97f5978fa8cdf2394933bfb612849525d7786e704,
		// 0:1558c14efb92f5113e5472e68d03ae72af97d285679fa0ad5a05cee607aaf93b,
		// 0:791afd21be72abe099049f6b23060ccb7141f144f397f7a4013d48caff6c90a0,
		// 0:dabb0a3dbb8f2da4428a4464d666296a72dd07c56d6f047524270dd12dec77d7,
		// 1:At2VbuZWVObia7_M4ck4E2ESBut-wWEsabJ92jZs9QtW,
		// 1:Apmkul5M9SIm_RWsLwdglIDKNTyPknw4T4_4AL6XaW5S,
		// 0:0d44d34804760478f465ffa7b837515b53b61eaa9aa3533f80e0eb7bbd5f3fd5,
		// 0:aa04a70267e0e790e8355bc70d79b74043aabc5368db987c4e28fe61683e1132,
		// 0:a794348f4cf0a64833f8c707a3ba5f67e80d931b4bb8b7b98bf7bd1f6d39c523,
		// 1:AsTdYmn70vphrRPlSTdgQB5iJq3SlaYj_QyVze6mUsge,
		// 0:844e14a4ee1e7409fff7f952e1e18bb6b8b166ac83ed07477fd35ef2919f2e26,
		// 0:bd124f0e44bdadc36f9e9c7595a6fb4fd82cbe0da154a56894ce3251f22aaa7d,
		// 0:87df0f724748880ea4e869084d394fcda0eb11ffa12e47d6e2c1ac0168140f58,
		// 1:AjH-j2nO6bplkgTZvLFiEUYrW2G4s02OZPqQLJVPcSPu,
		// 1:AiqStjMfAuY37ecmn5ON3XrQSuJFfcxnTJmrM4wbNoyi,
		// 0:fe6bba2dcfff43f66afc37913a7f74bfe45ad5abf78477df83c262d476491770,
		// 1:AlTJ_0PbS41UacTA2ZNlsDQvG8ge2rhRQu-WEtSukyC-,
		// 0:1337af4a6a3f880c10c664e5f5c111b4fc117101753cf8afc52da099e8199f48,
		// 0:4fbe415a057801314e220fa84e47b96edfc28b0e7874c7f582d37dc2ce8946f3,
		// 0:87bbb31a1278e5e2d4059f2b1e398c6d09f46d9c6296a39c8fe8e3ff169dc7a7,
		// 0:e4129df0e2716bd4812f7e0c051b0cce431eecf282307076bfe51f38a96abd16,
		// 0:2c5c094dcc04935a514d53a7555caa0429475f8161bfe3922afee8163559867d,
		// 1:AqabBoXfPkDi2a1TOEsze6GwsysXYWJ8ByKQMJgtTGfG,
		// 1:Ajvbz_ig5w83AlTthkaVIojGz-HFGW2YFqRfyCXPXNVE,
		// 1:AmbVQyZegz4ekb18J_x2hXAWB_rL54Pv-1lzJGhPGyfx,
		// 0:f87b845133ef5fc1ef9ab0b9839fcaaefbebf3038c8098f9e1d1be794e9ceefd,
		// 1:AtxY3-LAJO5a4zQqyVTySHmc8X5-Wj9XNOIoMo0ZIpsi,
		// 0:d8574c1f57dee434a331b4da64fd60fe648a1c425e66bc5747d0872f6ac4114f,
		// 0:7e6ef2a39a0897896838a366b573332a49bd76bac9231b43b7f3a0c8153c5629,
		// 1:AhN7pShCnBGTL9VyUBpNmBjs2djJRgXbFtJz-BqN4XOx,
		// 0:77daf2921ef8765692b53c17dd90f8c8b753cce4fdb648c23c4d65c22b8e57e8,
		// 0:c503a6439819fc8bddc99a2b6cec588dd4f5d0723a97aacd4b12a46de383715a,
		// 0:6d8c3610a1197aa085325b56de98825b2e3dbe635ba232576d542acb4d811062,
		// 1:AqZWeXHsD7kZ7myDKK_fw15s_9vm4-szysMFOXu5c7p4,
		// 0:a7a66f77a4e5eef5a561e0396d7f9016d474b573b74a7f27e9e656d99ff9f895,
		// 1:AitHFv1kYOa23tN82ZVU9fYSDwLSF-MQu8CujyWqEwcm,
		// 0:31f7ec0ae843427670ce8b5df0c34800a1f6c3a6761862cac859097ef4f9acfe,
		// 0:ed3c63317d2afd11f8c40b274edf3c3af176e29cdcd6ebba40fe721e2b1c0564,
		// 1:AtxdxZyHzXIK5kKloW3D7Egao-Ergi1J6iVgoP3Dr1dB,
		// 0:a4a75241f52c3b12e2ea72c8f283abba057cc680470173b5d93168fff9d9b907,
		// 0:238a3d94ee19a8aba299f846962cab3a6c00e0521d81f59dec6852b796fceb13,
		// 0:8bfef5126f7433c5f9ee0bd8202ed766f9763a9e94162fc103a79944bc6f3fb7,
		// 1:AorOpBK79ZPRGQvQDfqwJfOF-bVujQg8acaNYQFr1gJ4,
		// 0:0d5c1b8ba0729d73b10967e671e20363144f0cdb3b9ec59e326255f8d0590039,
		// 0:19498ab339f52cde56132cdcadfb893f0b42eec46c08cac33bca776cbb1a1e36,
		// 1:An9CHTO92Tt_XyEqDdTOjFqTtotGkHTACQWQc6BekUOk,
		// 1:Apr-FiqZgruhyRmZh0_7x-13umVLMT4OCFLIpujCaVE0,
		// 0:b001961ea10842519773076fce56cac040798898c823b96c07e6cca13df08a55,
		// 0:9390e5ad3ac041c8c72ab13d3014711a442589382f1b7be162e1331935c60c78,
		// 0:8fff12167d173d1b4598deb04b0ba82d562beec017436f846fbcf5c05edbf29f,
		// 0:161969d12b3a0967dd9ba2e5f3728478578fc7c96756613a3001f14b477df1db,
		// 1:AqMP2AixJJFHsOkQO-uBUpHV0P1CuABhZgBhKmTF0HWE,
		// 1:AqPBRLk-L9eOQSfQhuFhwO27lAGFajGe7ir9GXfCMTC0,
		// 1:Aj9zYYn6PODZ0ShD9nUw4OI8UBLMdqSgWArP3bHnyZP4,
		// 1:Amz2ZYJBpNE990LRXFoPHSGoIBUK0GaU4uxRy2t-i6nk,
		// 0:e6f1f04ae8b2b1746591f73df60c887fb9b5ee9e1cfc67d89623d6af5b208a87,
		// 0:4e55c50c1020ac15ea4974b6be3f224a85281a17c2feb62c55bfc0ac0ca3d05d,
		// 1:AtLy8k8whEMBjiTuD4j1uNTk38WLTCuK5BmIrMC9LhdV,
		// 1:AuOeNRsNgP-mHdngtc0hG5jWl2mFiW9-wRKvEy6U3kUO,
		// 0:936779ce71df11e1154ecb5511234660cdab1b9a9d35eb4671f80e9843590fc7,
		// 0:032fbea5299ce619d53b4b87561ce7fd70ff4c262eda82e186ac97f1dfbd0daf,
		// 1:AowbpSd8FynMhFh4eVwMlo83eaPfMPGzIUjhpV-muu86,
		// 0:3e05bf734fe81adda8b8458bc8688ebbe0dc7813875c50b55ad326ff2cdb3dec,
		// 0:08a22deb0b6cb30c37823e643a7f58c77c989e963aaf4a1e68cd6de5f70ef4a0,
		// 1:AkG8SKKQSjIDddz2payEgED9VKTKfT2w-g2rJbcKG9Kx,
		// 0:a753d386f195ce9a0aa9d009df3da2cd8fb79436f3802673fbe2c1f5e1f83326,
		// 0:392fc7a03cd89071d94cba5c72c6ea6f82b298ba49b13d6cd6f8967a17ea89de,
		// 0:596dda2fe3126c62a36f96d3271699f14323f53b8bb4f82a83d24ef1bc9c0df2,
		// 0:199bb88f1a27748ee77478467b663e602b96ee85ebf1d618083981b1f355fffb,
		// 1:Atq2093rEqk-IONuVxxIUUF_S3RPa9I2_zWoAxSBnM7S,
		// 1:AtgAHF5IU3VEOZXU1IAgXflrkjtrPQx-W2jq7jeFX1V2,
		// 0:6ccbb0d557aed68c0b208380de6a0e40c80713ffb67b2632f6cfc2eda32a535e,
		// 0:82c3eef271fc49399717ea2a6e998bf4688228f1452fef4a9b919c639aa28e67,
		// 0:bbc405c203fc614b1c121c4b31095a6b2a0e3f391de6ee7d29d4f1cd118796e8";
		// String[] str = s.split(",");
		//
		// for (String v : str) {
		// if (v.trim().startsWith("0")) {
		// tokens.add(v.trim());
		// }
		// }
		//
		// PushTask task = new PushTask(msg, userId, tokens);
		//
		// ThreadPoolExecutor.execute(task);
		// }
		// else if (typeId == 4) {
		// Set<String> tokens = new HashSet<String>();
		//
		// tokens.add("0:e5daece76566226b162ea9cd6992cb6e63825229fb5b3a748aaacfa5d6c2c30f");
		// tokens.add("0:4e55c50c1020ac15ea4974b6be3f224a85281a17c2feb62c55bfc0ac0ca3d05d");
		// tokens.add("0:199bb88f1a27748ee77478467b663e602b96ee85ebf1d618083981b1f355fffb");
		// tokens.add("0:fe6bba2dcfff43f66afc37913a7f74bfe45ad5abf78477df83c262d476491770");
		// tokens.add("0:0f02bfa9cf2c47f378c86ab32ba8b49c2a92568a934afedd083528c79fa42bad");
		//
		// PushTask task = new PushTask(msg, userId, tokens);
		//
		// ThreadPoolExecutor.execute(task);
		// } else if (typeId == 5) {
		//
		// try {
		// ApnsPusher.APNSPush("87BBB31A1278E5E2D4059F2B1E398C6D09F46D9C6296A39C8FE8E3FF169DC7A7".toLowerCase(),
		// msg, userId);
		//
		// } catch (Exception e) {
		// e.printStackTrace();
		// }
		// }

		logger.info("推送结束--!");
	}
}
